package com.yanxinim.cn.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yanxinim.cn.R;
import com.yanxinim.cn.adapter.BankNameAdapter;
import com.yanxinim.cn.bean.redpacket.BankInfoBean;
import com.yanxinim.cn.call.OnClickListener;
import com.yanxinim.cn.util.input.HideSoftInputUtil;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.ActionBar;

/**
 * Created by ChenXL on 2019/4/2.
 * <p>
 * 下拉PopupWindow组件
 */

public class PulldownPopupWindow {

    CustomPopupWindow popupWindow;

    Context mContext;

    List<BankInfoBean> mDatas = new ArrayList<>();

    View mView;

    TextView mTextView;
    ImageView mImageView;

    boolean isSelectPosiOne = true;

    OnClickListener onClickListener;

    public PulldownPopupWindow(Context context, List<BankInfoBean> datas, View view, TextView textView, ImageView imageView, boolean isSelectPosiOne) {
        this.mContext = context;
        this.mDatas = datas;
        this.mView = view;
        this.mTextView = textView;
        this.mImageView = imageView;
        this.isSelectPosiOne = isSelectPosiOne;
        HideSoftInputUtil.HideKeyboard(mView);
        createPopupWindow();
    }

    public PulldownPopupWindow(Context context, List<BankInfoBean> datas, View view, boolean isSelectPosiOne, OnClickListener onClickListener) {
        this.mContext = context;
        this.mDatas = datas;
        this.mView = view;
        this.isSelectPosiOne = isSelectPosiOne;
        this.onClickListener = onClickListener;
        HideSoftInputUtil.HideKeyboard(mView);
        createPopupWindow();
    }

    private void createPopupWindow() {
        @SuppressLint("InflateParams")
        View converView = LayoutInflater.from(mContext).inflate(R.layout.listview_simple, null);
        ListView listView = converView.findViewById(R.id.listview_simple);

        BankNameAdapter adapter = new BankNameAdapter(mContext);
        listView.setAdapter(adapter);
        adapter.setDatas(mDatas);
        adapter.notifyDataSetChanged();

        popupWindow = new CustomPopupWindow(listView, mView.getWidth(), ActionBar.LayoutParams.WRAP_CONTENT, true);
        // 设置外部可点击
        popupWindow.setOutsideTouchable(true);
        // 给popupWindow添加背景，点击外部可将其隐藏
        popupWindow.setBackgroundDrawable(null);

        // 选中某个item
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (isSelectPosiOne) {
                    if (mTextView != null){
                        mTextView.setText(mDatas.get(position).getName());
                    }
                    if (mImageView != null){
                        mImageView.setImageResource(BankNameAdapter.getBankLogo(mDatas.get(position).getCode()));
                    }
                }
                if (onClickListener != null) {
                    mView.setTag(mDatas.get(position));
                    onClickListener.onClick(mView);
                }
                popupWindow.dismiss();
            }
        });

        popupWindow.showAsDropDown(mView);
    }
}
